﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace DATN.Account
{
    public partial class Profiles : System.Web.UI.Page
    {
        string cnnString;
        SqlConnection cnn;
        SqlCommand cmd;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["Us_Id"] != null)
                {
                    //Bind data vào GridView
                    BindData();
                }
                else
                {
                    Response.Redirect("../Account/Login.aspx");
                }
                Validate();
            }
        }

        protected void BindData()
        {
            try
            {

                Response.Write(Txt_Mail.Text);
                cnnString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
                cnn = new SqlConnection(cnnString);
                cnn.Open();

                String queryString = "SELECT * FROM Users WHERE Us_Id = @Us_Id";
                
                cmd = new SqlCommand(queryString, cnn);

                cmd.Parameters.Add("@Us_Id", SqlDbType.Int);
                cmd.Parameters["@Us_Id"].Value = Convert.ToInt16(Session["Us_Id"].ToString());  

                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Label_Profile.Visible = true;
                    Label_Profile.Text = "Trang cá nhân: " + reader["FirstName"].ToString();
                    Txt_Mail.Text = reader["Mail"].ToString();
                    Txt_Firstname.Text = reader["FirstName"].ToString();
                    Txt_Lastname.Text = reader["LastName"].ToString();
                    Txt_Birthday.Text = reader["Birthday"].ToString();
                    Txt_Phone.Text = reader["Phone"].ToString();
                    Txt_Address.Text = reader["Address"].ToString();
                    Txt_Company.Text = reader["Company"].ToString();
                }
                //DateTime.Parse(reader["Birthday"].ToString()).ToShortDateString()
                cnn.Close();
            }
            catch (Exception ex)
            {
                Response.Write(ex);
                cnn.Close();
            }
        }

        protected void Btn_Update_Click(object sender, EventArgs e)
        {            
            try
            {                
                cnnString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
                cnn = new SqlConnection(cnnString);
                cnn.Open();

                cmd = cnn.CreateCommand();
                cmd.CommandText =  "UPDATE Users SET [Mail] = @Mail,"
                                + "[FirstName] = @FirstName, [LastName] = @LastName,[Birthday] = @Birthday, "
                                + "[Phone] = @Phone, [Address] = @Address, [Company] = @Company where Us_Id= @Us_Id";

                cmd.Parameters.Add("@Mail", SqlDbType.NVarChar, 50);
                cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 50);
                cmd.Parameters.Add("@LastName", SqlDbType.NVarChar, 50);
                cmd.Parameters.Add("@Birthday", SqlDbType.Date);               
                cmd.Parameters.Add("@Phone", SqlDbType.VarChar, 16);                
                cmd.Parameters.Add("@Address", SqlDbType.NVarChar, 150);                
                cmd.Parameters.Add("@Company", SqlDbType.NVarChar, 50);
                cmd.Parameters.Add("@Us_Id", SqlDbType.Int);
                
                cmd.Parameters["@Mail"].Value = Txt_Mail.Text;
                cmd.Parameters["@FirstName"].Value = Txt_Firstname.Text;
                cmd.Parameters["@LastName"].Value = Txt_Lastname.Text;
                cmd.Parameters["@Birthday"].Value = Txt_Birthday.Text;                
                cmd.Parameters["@Phone"].Value = Txt_Phone.Text;               
                cmd.Parameters["@Address"].Value = Txt_Address.Text;                
                cmd.Parameters["@Company"].Value = Txt_Company.Text;
                cmd.Parameters["@Us_Id"].Value = Convert.ToInt16(Session["Us_Id"].ToString());   

                cmd.ExecuteNonQuery();

                Label_success.Visible = true;
                cnn.Close();
            }
            catch (Exception ex)
            {
                Response.Write(ex);
            }
        }

        protected void Btn_Back_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/mds.UserInterface/InternalIndex.aspx");
        }
    }
}